package com.renjibo.childred.databases;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.renjibo.childred.modle.GoodsBean;
import com.renjibo.childred.modle.GoodsChild;

import java.util.ArrayList;
import java.util.List;

/**
 * 任继波
 * Created by Administrator on 2017/2/27.
 */

public class UserDao {
    private final String GROUD="groud";
    private final String CHILD="child";
    private MySqlite mySqlite;
    public UserDao(MySqlite mySqlite){
        this.mySqlite=mySqlite;
    }
    //添加数据
    public void addUser(final GoodsBean goodsBean){
//        groupChex integer,goneView integer,name vafrchar(20),countMonery double(20)
//        String table String nullColumnHack, ContentValues values
        new Thread(new Runnable() {
            @Override
            public void run() {
                SQLiteDatabase db = mySqlite.getWritableDatabase();
                Cursor cursor = db.query(GROUD, null, null, null, null, null, null);
                if(!cursor.moveToNext()){
                    ContentValues con=new ContentValues();
                    con.put("name",goodsBean.getName());
                    con.put("countMonery",goodsBean.getCountMonery());
                    if(goodsBean.isGroupChex()){
                        con.put("groupChex",1);
                    }else{
                        con.put("groupChex",0);
                    }
                    if(goodsBean.isGoneView()){
                        con.put("goneView",1);
                    }else{
                        con.put("goneView",0);
                    }
                    db.insert(GROUD,null,con);
                }
                List<GoodsChild> good1 = goodsBean.getGood();
                //,childChe integer,imageUrl varchar(20),name varchar(20),price double(20),num integer,gridId integer
                for(GoodsChild gc:good1){
                    ContentValues conChild=new ContentValues();
                    if(gc.isChildChe()){
                        conChild.put("childChe",1);
                    }else{
                        conChild.put("childChe",0);
                    }
                    conChild.put("imageUrl",gc.getImageUrl());
                    conChild.put("name",gc.getName());
                    conChild.put("price",gc.getPrice());
                    conChild.put("num",gc.getNum());
                    conChild.put("gridId",1);
                    db.insert(CHILD,null,conChild);
                }
                db.close();
            }
        }).start();
    }
    //c查询数据
    public List<GoodsBean> chaAll(){
        SQLiteDatabase db = mySqlite.getReadableDatabase();
        Cursor cursor = db.query(GROUD, null, null, null, null, null, null);
        List<GoodsBean> groudArray=new ArrayList<>();
        List<GoodsChild> childArray=new ArrayList<>();
        boolean groupChex=false;
        while(cursor.moveToNext()){
//            String name, List<GoodsChild> good, boolean groupChex
            int groudId=cursor.getInt(cursor.getColumnIndex("gridId"));
            Cursor cursorChild = db.query(CHILD, null, "gridId=?", new String[]{groudId+""}, null, null, null);
            while(cursorChild.moveToNext()){
//                boolean childChe, String imageUrl, String name, double price, int num
//                childChe integer,imageUrl varchar(20),name varchar(20),price double(20),num integer,gridId integer
                int childChe = cursorChild.getInt(cursorChild.getColumnIndex("childChe"));
//                int childChe = 0;
                boolean childboo=false;
                if(childChe==1){
                    childboo=true;
                }else{
                    childboo=false;
                }
//                boolean childChe, String imageUrl, String name, double price, int num
                GoodsChild goodsChild=new GoodsChild(childboo,cursorChild.getString(cursorChild.getColumnIndex("imageUrl")),
                        cursorChild.getString(cursorChild.getColumnIndex("name")),cursorChild.getDouble(cursorChild.getColumnIndex("price"))
                ,cursorChild.getInt(cursorChild.getColumnIndex("num")));
                childArray.add(goodsChild);
            }
            if(cursor.getInt(cursor.getColumnIndex("groupChex"))==1){
                groupChex=true;
            }else{
               groupChex=false;
            }
            GoodsBean gb=new GoodsBean(cursor.getString(cursor.getColumnIndex("name")),childArray,groupChex);
            groudArray.add(gb);
        }
        db.close();
        return groudArray;
    }
}
